<?php
chdir("/home/cm/public_html/crons/");
include('../config.php');

$log = new Logging();

$log->lfile('../logs/admin_daily_summary_' . date('Y_m_d') . '.log');
$log->lwrite('Start');

$change_obj = new change_obj();
$task_obj = new task_obj();
$record_obj = new record_obj();
$user_obj = new user_obj();
$client_obj = new client_obj();

$filter = array();
$filter['type'] = CHANGES_TYPE_TASK;
$tasksIds = $change_obj->get_changes($filter, '`date` ASC', 'rel_id', 'rel_id');

$m_task = '';
if (error_obj::isError($tasksIds)) {
    $log->lwrite('Nepodarilo sa ziskat zoznam Id task na vypis');
} else {
    if (!empty($tasksIds)) {
        foreach ($tasksIds as $value) {
            $exist = $task_obj->getTaskById($value['rel_id']);
            if (error_obj::isError($exist)) {
                $log->lwrite('Nepodarilo sa ziskat task - error - ' . $value['rel_id']);
            } elseif (empty($exist)) {
                $log->lwrite('Nepodarilo sa ziskat task - neexistuje - ' . $value['rel_id']);
            } else {
                $client_data = $client_obj->getClient($exist['client'], array('name'));
                if (empty($client_data) || error_obj::isError($client_data))
                    $client_data['name'] = "";                
                $filter = array();
                $filter['type'] = CHANGES_TYPE_TASK;
                $filter['rel_id'] = $value['rel_id'];
                $changes = $change_obj->get_changes($filter, 'status ASC, `date` ASC');

                if (error_obj::isError($changes)) {
                    $log->lwrite('Nepodarilo sa ziskat zoznam zmien pre task s id:' . $value['rel_id']);
                } else {
                    $data = array();
                    $data['name'] = $exist['title'];
                    $data['root'] = WWW;
                    $data['absPath'] = FILE;
                    foreach ($changes as $key => $change) {
                        if (!empty($change['user_id'])){
                            $user_data = $user_obj->getUserById($change['user_id'], array('name', 'surname', 'client'));
                            if (!error_obj::isError($user_data) && !empty($user_data)){
                                $changes[$key]['user_data'] = $user_data;
                            }
                        }
                    }
                    $data['client_data'] = $client_data;
                    $data['changes'] = $changes;
                    $data['type'] = CHANGES_TYPE_TASK;
                    $GLOBALS['smarty']->template_dir = PATH_TO_TEMPLATES . 'includes/';
                    $GLOBALS['smarty']->assign("data", $data);
                    $m_task .= $GLOBALS['smarty']->fetch('admin_summary.tpl');
                    $m_task .= '<br/>';
                }
            }
        }
    }
    if (!empty($m_task)) {
        $m_task = '<div style="padding: 5px; border: 1px solid black; margin-bottom:20xp"><h4>TASKS:</h4>' . $m_task . '</div>';
    }
}

$filter = array();
$filter['type'] = CHANGES_TYPE_TSDR;
$tsdrsIds = $change_obj->get_changes($filter, '`date` ASC', 'rel_id', 'rel_id');

$m_tsdr = '';
if (error_obj::isError($tsdrsIds)) {
    $log->lwrite('Nepodarilo sa ziskat zoznam Id tsdr na vypis');
} else {
    if (!empty($tsdrsIds)) {
        foreach ($tsdrsIds as $value) {
            $exist = $record_obj->getRecordById($value['rel_id']);
            if (error_obj::isError($exist)) {
                $log->lwrite('Nepodarilo sa ziskat tsdr - error - ' . $value['rel_id']);
            } elseif (empty($exist)) {
                $log->lwrite('Nepodarilo sa ziskat tsdr - neexistuje - ' . $value['rel_id']);
            } else {
                $client_data = $client_obj->getClient($exist['client'], array('name'));
                if (empty($client_data) || error_obj::isError($client_data))
                    $client_data['name'] = "";
                $filter = array();
                $filter['type'] = CHANGES_TYPE_TSDR;
                $filter['rel_id'] = $value['rel_id'];
                $changes = $change_obj->get_changes($filter, 'status ASC, `date` ASC');

                if (error_obj::isError($changes)) {
                    $log->lwrite('Nepodarilo sa ziskat zoznam zmien pre tsdr s id:' . $value['rel_id']);
                } else {
                    $data = array();
                    $data['name'] = $exist['description'];
                    $data['root'] = WWW;
                    $data['absPath'] = FILE;
                    foreach ($changes as $key => $change) {
                        if (!empty($change['user_id'])){
                            $user_data = $user_obj->getUserById($change['user_id'], array('name', 'surname', 'client'));
                            if (!error_obj::isError($user_data) && !empty($user_data)){
                                $changes[$key]['user_data'] = $user_data;
                            }
                        }
                    }
                    $data['client_data'] = $client_data;
                    $data['changes'] = $changes;
                    $data['type'] = CHANGES_TYPE_TSDR;
                    $GLOBALS['smarty']->template_dir = PATH_TO_TEMPLATES . 'includes/';
                    $GLOBALS['smarty']->assign("data", $data);
                    $m_tsdr .= $GLOBALS['smarty']->fetch('admin_summary.tpl');
                    $m_tsdr .= '<br/>';
                }
            }
        }
    }
    if (!empty($m_tsdr)) {
        $m_tsdr = '<div style="padding: 5px; border: 1px solid black; margin-bottom:20xp"><h4>TSDR:</h4>' . $m_tsdr . '</div>';
    }
}

$message = $m_task . $m_tsdr;

if (!empty($message)) {

    $notify_obj = new notify_obj();
    $recipients_emails = array();
    $recipients_emails[] = 'sec@alfac.sk';
    $recipients_emails[] = 'veliky@alfac.sk';
    $recipients_emails[] = 'jpogadl@gmail.com';
    
    $subject = "CM: Daily summary";
    
    $send_result = $notify_obj->send($recipients_emails, $message, $subject);
    if (error_obj::isError($send_result))
        $log->lwrite('Nepodarilo sa odoslat emaily');
    else {
        $change_obj->clear_table();
    }
}


$log->lwrite('Stop');
$log->lwrite('------------------------------------------');

$log->lclose();
?>
